package listeners;

import handlers.WeatherStationHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Listener for the Weather Stations
 * 
 * @author Nils Berlijn & Henderikus Harms
 */	
public class WeatherStationListener implements Runnable
{
	private boolean listen;
	private ServerSocket socket;
	private static final int PORT = 7789;

	/**
	 * Initialize the WeatherStationListener
	 * Listen to true
	 * Setup the ServerSocket
	 */
	public WeatherStationListener()
	{
		listen = true;
		
		try 
		{
			socket = new ServerSocket(PORT);
		} 
		catch(IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Run the Weather Station Listener
	 * 
	 * @param weatherStationSocket Accept the socket
	 * @param thread Make a Thread that will handle the incoming connections
	 */
	@Override
	public void run()
	{
		System.out.println("Weather Station Listener running");
		
		Socket weatherStationSocket;
		
		while(listen)
		{
			try 
			{
				weatherStationSocket = socket.accept();
				
				Thread thread = new Thread(new WeatherStationHandler(weatherStationSocket));
				thread.start();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}
}
